public class Plot2Tex {

  public static void main(def args) {
    // Insert results comparing with MSJDBC
    cmpcycle "jdbccmp", "MSJDBC", MSJDBC_SECOND, maincontexts, "subsubsection", "paragraph"
    // Insert results comparing with WJDBC
    cmpcycle "jdbccmp", "WJDBC", WJDBC_SECOND, maincontexts, "subsubsection", "paragraph"

    cmpcycle "cachecmp", "CJDBC_I", ["CJDBC_SM"], cachecontexts, "subsection", "subsubsection"
    cmpcycle2 "cachecmp", "CJDBC_I", ["CJDBC_S"], ["100"], "subsubsection"

    cmpcycle "delayccmp", "MSJDBC", MSJDBC_SECOND, maincontexts[0..1], "paragraph", "subparagraph"
    cmpcycle "delayccmp", "WJDBC", WJDBC_SECOND, maincontexts[0..1], "paragraph", "subparagraph"
    cmpcycle "delaylcmp", "MSJDBC", MSJDBC_SECOND, maincontexts[0..1], "paragraph", "subparagraph"
    cmpcycle "delaylcmp", "WJDBC", WJDBC_SECOND, maincontexts[0..1], "paragraph", "subparagraph"
  }

  def static cmpcycle(cmp, first, listOfSeconds, listOfContexts, section1type, section2type) {
    File fout = new File("images/results/${cmp}-${first}.tex")
    fout.write("")
    listOfContexts.each { context ->
      fout.append("\\${section1type}{${replacecontext(context)}}\n", "utf-8")
      listOfSeconds.each { second ->
        plot2tex cmp, first, second, context, fout, section2type
      }
    }
  }
  def static cmpcycle2(cmp, first, listOfSeconds, listOfContexts, sectiontype) {
    File fout = new File("images/results/${cmp}-${first}.tex")
    listOfContexts.each { context ->
      listOfSeconds.each { second ->
        plot2tex cmp, first, second, context, fout, sectiontype
      }
    }
  }

  def static plot2tex(cmp, first, second, context, fout, sectiontype) {
    def figname = "${cmp}-${first}-${second}-${context}"
    def content = "\\${sectiontype}{\$${second}\$} ~\\newline\n\n" \
      + "A Figura \\ref{fig:${figname}} apresenta os resultados " \
      + "da comparação do desempenho de \$${first}\$ e de \$${second}\$, " \
      + "no contexto ${replacecontext(context)}.\n\n" \
      + "\\input{images/results/${figname}}\n" \
      + "\\input{chapters/resultados/${cmp}/${first}-${second}-${context}}\n\n"

    fout.append(content, "utf-8")
  }

  def static replacecontext(context) {
    if(context.equals("Actualizacao"))
      "Actualização"
    else if(context.equals("Insercao"))
      "Inserção"
    else if(context.equals("Remocao"))
      "Remoção"
    else if(context.equals("10"))
      "Fetch size 10\\%"
    else if(context.equals("20"))
      "Fetch size 20\\%"
    else if(context.equals("50"))
      "Fetch size 50\\%"
    else if(context.equals("75"))
      "Fetch size 75\\%"
    else if(context.equals("100"))
      "Fetch size 100\\%"
    else
      context
  }

  def static maincontexts = ["Actualizacao", "Leitura", "Insercao", "Remocao"]
  def static cachecontexts = ["10", "20", "50", "75", "100"]
  def static MSJDBC_SECOND = ["CJDBC_I", "CJDBC_S", "WJDBC"]
  def static WJDBC_SECOND = ["CJDBC_I", "CJDBC_S"]
}
